package com.github.wujichen158.andemusic.playernbt;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.util.INBTSerializable;

public class MusicTypeProvider implements ICapabilityProvider, INBTSerializable<NBTTagCompound> {

    @CapabilityInject(IMusicCapability.class)
    public static Capability<IMusicCapability> CAP = null;
    public IMusicCapability musicCapability;

    public MusicTypeProvider() {
        this.musicCapability = new MusicCapability();
    }

    public MusicTypeProvider(IMusicCapability c) {
        this.musicCapability = c;
    }

    public static IMusicCapability get(EntityPlayer player) {
        if (player != null) {
            if (player.hasCapability(CAP, null)) {
                return player.getCapability(CAP, null);
            }
        }
        return null;
    }

    @Override
    public NBTTagCompound serializeNBT() {
        return this.musicCapability.saveNBTData();
    }

    @Override
    public void deserializeNBT(NBTTagCompound nbt) {
        this.musicCapability.loadNBTData(nbt);
    }

    @Override
    public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
        return CAP != null && capability == CAP;
    }

    @Override
    public <T> T getCapability(Capability<T> capability, EnumFacing enumFacing) {
        if (CAP != null && capability == CAP) {
            return (T) this.musicCapability;
        }
        return null;
    }

}
